package com.work_attendance.common.utils;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecurityUtils {
    /**
     * md5加密
     */
    public static String encrptyPassword(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5=MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder=new BASE64Encoder();//不使用base64处理可能会乱码
        String result = base64Encoder.encode(md5.digest(password.getBytes("utf-8")));
        return result;
    }
    /**
     * 和数据库密码进行比对
     */

    public static boolean checkPassword(String inputPwd,String dbPwd) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String s = encrptyPassword(inputPwd);
        if(s.equals(dbPwd)){
            return true;
        }else{
            return false;
        }
    }
}
